package com.bobo.controller.back.photo;

import com.alibaba.druid.util.StringUtils;
import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.annotations.RequestMethod;
import com.bobo.controller.base.BaseServlet;
import com.bobo.model.Category;
import com.bobo.model.Photo;
import com.bobo.service.photo.IPhotoService;
import com.bobo.service.photo.PhotoServiceImpl;
import com.bobo.util.UploadFiles;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by bobo on 2016/10/24.
 */
@WebServlet(name = "Photo", urlPatterns = {"/admin/photo/*"})
public class PhotoController extends BaseServlet {

    private IPhotoService photoService = new PhotoServiceImpl();
    private UploadFiles uploadFiles = new UploadFiles();

    /**
     * 新增get
     * @return 页面路径
     */
    @RequestMapping("/add")
    public String add() {
        return "jsp/back/photo/add";
    }

    /**
     * 新增post
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response) {

        try {
            Object[] obj = uploadFiles.upload(request).get("fileName");

            if (obj!=null && obj.length > 0) {
                Photo photo = new Photo();
                photo.setImg((String) obj[0]);
                photo.setStatus(1);
                photoService.create(photo);
            }else {
                return "error";
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/photo/headerImg";
    }


    /**
     * 删除get
     * @param request
     * @return
     */
    @RequestBody
    @RequestMapping("/delImg")
    public String del(HttpServletRequest request) {
        try {
            int id = Integer.parseInt(request.getParameter("id"));

            Photo photo = photoService.getPhoto(id);
            if (photoService.delete(id)) {
                uploadFiles.delFile(request, photo.getImg());
                return "true";
            }

        } catch (Exception e) {
            e.fillInStackTrace();
        }
        return "false";
    }

    /**
     * 获取单个图片 get
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/get")
    public String get(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        Photo photo = photoService.getPhoto(id);
        request.setAttribute("photo", photo);
        return "jsp/back/photo/get";
    }

    /**
     * 更新图片 get
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/upd")
    public String upd1(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        Photo photo = photoService.getPhoto(id);
        request.setAttribute("photo", photo);
        return "jsp/back/photo/upd";
    }

    /**
     * 更新图片 post
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/upd", method = RequestMethod.POST)
    public String upd(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object[]> map = uploadFiles.upload(request);
            Photo photo;
            Object[] ids =  map.get("id");
            if(ids!=null&&ids.length>0){
                String pid = (String) ids[0];
                photo = photoService.getPhoto(Integer.valueOf(pid));
                Object[] obj = map.get("fileName");
                if(obj==null){
                    return "error";
                }else {
                    if (obj.length > 0) {
                        uploadFiles.delFile(request, photo.getImg());
                        photo.setImg((String) obj[0]);
                        photoService.update(photo);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/photo/headerImg";
    }

    /**
     * 图片列表 get
     * @param request
     * @return
     */
    @RequestMapping("/headerImg")
    public String getHeaderImgs(HttpServletRequest request) {
        int current = 1;
        int pageSize = 5;
        if (request.getParameter("current") != null && !StringUtils.equals(request.getParameter("current"), "")) {
            current = Integer.valueOf(request.getParameter("current"));
        }
        if (request.getParameter("pageSize") != null && !"".equals(request.getParameter("pageSize"))) {
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
        }
        request.setAttribute("headers", photoService.getPage(current, pageSize));

        return "jsp/back/photo/getHeaderImgs";
    }
    @RequestBody
    @RequestMapping("/changeStatus")
    public String changeStatus(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Photo photo = photoService.getPhoto(id);
        int status = Integer.parseInt(request.getParameter("status"));
        photo.setStatus(status);
        photoService.update(photo);
        return "true";
    }

    /**
     * -1状态图片列表 get
     * @param request
     * @return
     */
    @RequestMapping("/cp")
    public String cp(HttpServletRequest request) {
        int current = 1;
        int pageSize = 5;
        if (request.getParameter("current") != null && !StringUtils.equals(request.getParameter("current"), "")) {
            current = Integer.valueOf(request.getParameter("current"));
        }
        if (request.getParameter("pageSize") != null && !"".equals(request.getParameter("pageSize"))) {
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
        }
        request.setAttribute("headers", photoService.getCp(current, pageSize));

        return "jsp/back/photo/getHeaderImgs";
    }
}
